import axios from 'axios'

const state = {
  tasks: [],
  currentTask: null
}

const mutations = {
  SET_TASKS(state, tasks) {
    state.tasks = tasks
  },
  SET_CURRENT_TASK(state, task) {
    state.currentTask = task
  },
  ADD_TASK(state, task) {
    state.tasks.push(task)
  },
  UPDATE_TASK(state, updatedTask) {
    const index = state.tasks.findIndex(t => t.id === updatedTask.id)
    if (index !== -1) {
      state.tasks.splice(index, 1, updatedTask)
    }
  },
  REMOVE_TASK(state, taskId) {
    state.tasks = state.tasks.filter(t => t.id !== taskId)
  }
}

const actions = {
  async fetchTasks({ commit }) {
    try {
      const response = await axios.get('/api/migrations')
      commit('SET_TASKS', response.data)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async createTask({ commit }, taskData) {
    try {
      const response = await axios.post('/api/migrations', taskData)
      commit('ADD_TASK', response.data)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async updateTaskStatus({ commit }, { id, status, progress, errorMessage }) {
    try {
      const params = new URLSearchParams()
      params.append('status', status)
      if (progress !== undefined) params.append('progress', progress)
      if (errorMessage !== undefined) params.append('errorMessage', errorMessage)
      
      const response = await axios.put(`/api/migrations/${id}/status`, null, { params })
      commit('UPDATE_TASK', response.data)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async resetTask({ commit }, id) {
    try {
      const response = await axios.put(`/api/migrations/${id}/reset`)
      commit('UPDATE_TASK', response.data)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async pauseTask({ commit }, id) {
    try {
      const response = await axios.put(`/api/migrations/${id}/pause`)
      commit('UPDATE_TASK', response.data)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async resumeTask({ commit }, id) {
    try {
      const response = await axios.put(`/api/migrations/${id}/resume`)
      commit('UPDATE_TASK', response.data)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async stopTask({ commit }, id) {
    try {
      const response = await axios.put(`/api/migrations/${id}/stop`)
      commit('UPDATE_TASK', response.data)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async executeTask({ commit }, id) {
    try {
      const response = await axios.post(`/api/migrations/${id}/execute`)
      return response.data
    } catch (error) {
      throw error
    }
  },
  
  async deleteTask({ commit }, id) {
    try {
      await axios.delete(`/api/migrations/${id}`)
      commit('REMOVE_TASK', id)
      return true
    } catch (error) {
      throw error
    }
  }
}

const getters = {
  allTasks: state => state.tasks,
  currentTask: state => state.currentTask,
  tasksByStatus: (state) => (status) => {
    return state.tasks.filter(task => task.status === status)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}